﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;

namespace TrendingReader.V2
{
    public partial class FrmMain : Telerik.WinControls.UI.RadForm
    {
        //private V2.FrmGetStories _frmGetStories;
        private V2.FrmGetStoriesAsync _frmGetStoriesAsync;
        private FrmReader _frmReader;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void BtnGetStories_Click(object sender, EventArgs e)
        {
            //if (this._frmGetStories == null)
            //    this._frmGetStories = new V2.FrmGetStories();

            //this._frmGetStories.Show();
            //this._frmGetStories.FormClosed += FrmGetStories_FormClosed;

            if (this._frmGetStoriesAsync == null)
                this._frmGetStoriesAsync = new FrmGetStoriesAsync();
            this._frmGetStoriesAsync.Show();
            this._frmGetStoriesAsync.FormClosed += FrmGetStories_FormClosed;
            this.WindowState = FormWindowState.Minimized;
            
            BtnGetStories.Enabled = false;
        }

        private void BtnStoriesReader_Click(object sender, EventArgs e)
        {
            if (this._frmReader == null)
                this._frmReader = new FrmReader();

            this._frmReader.Show();
            this._frmReader.FormClosed += FrmStoriesReader_FormClosed;
            BtnStoriesReader.Enabled = false;
            this.WindowState = FormWindowState.Minimized;
        }

        private void FrmGetStories_FormClosed(object sender, FormClosedEventArgs e)
        {
            BtnGetStories.Enabled = true;
            //this._frmGetStories.Dispose();
            //this._frmGetStories = null;
            this._frmGetStoriesAsync.Dispose();
            this._frmGetStoriesAsync = null;
            this.WindowState = FormWindowState.Normal;
        }

        private void FrmStoriesReader_FormClosed(object sender, FormClosedEventArgs e)
        {
            BtnStoriesReader.Enabled = true;
            this._frmReader.Dispose();
            this._frmReader = null;
            this.WindowState = FormWindowState.Normal;
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this._frmGetStoriesAsync != null || this._frmReader != null)
            {
                if (RadMessageBox.Show("We are running, Are you sure you want to force close?", "Hey dude!",
                    MessageBoxButtons.YesNo, RadMessageIcon.Question,
                    "If you say Yes, we'll force close all our opening form.") == DialogResult.No)
                {
                    e.Cancel = true;
                }
                else
                {
                    Application.ExitThread();
                }
            }
            else
            {
                if (RadMessageBox.Show("Are you sure you want to close?", "Hey dude!",
                    MessageBoxButtons.YesNo, RadMessageIcon.Question,
                    "If you say Yes, we'll close this program.") == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
                else
                {
                    Application.ExitThread();
                }
            }
        }
    }
}
